package com.eos.control.action.customer;

import java.util.ArrayList;
import java.util.List;

import com.eos.common.tool.DataCacheUtil;
import com.eos.common.tool.PageModelUtil;
import com.eos.control.action.base.AbstractBaseAction;
import com.eos.orm.mybatis.model.SysCustomer;
import com.hf.utils.StringUtil;

public class LookUpCustomerListAction extends AbstractBaseAction {
	private static final long serialVersionUID = -4088369892871212649L;
	private String customerName;
	private String nickName;
	private String customerType;
	private Integer pageNum;
	private Integer numPerPage;
	private Integer totalCount;
	private List<SysCustomer> sysCustomerList;
	
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getCustomerName() {
		return customerName;
	}

	public String getNickName() {
		return nickName;
	}

	public String getCustomerType() {
		return customerType;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public void setCustomerType(String customerType) {
		this.customerType = customerType;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(Integer numPerPage) {
		this.numPerPage = numPerPage;
	}

	public Integer getTotalCount() {
		return totalCount;
	}

	public List<SysCustomer> getSysCustomerList() {
		return sysCustomerList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String execute() {
		try {
			if (null == pageNum || pageNum == 0)
				pageNum = 1;
			if (numPerPage == null || numPerPage == 0)
				numPerPage = PAGE_SIZE;
			if(StringUtil.isEmpty(customerType))
				customerType = "-1";
			List<SysCustomer> customerList = DataCacheUtil.getAllSysCustomer();
			List<SysCustomer> rtnList = new ArrayList<SysCustomer>();
			if(!StringUtil.isEmpty(customerName)){
				if(null != customerList && customerList.size() >0){
					for(SysCustomer sc : customerList){
						if(sc.getCustomerName().indexOf(customerName) >=0){
							rtnList.add(sc);
						}
					}
				}
			}else{
				rtnList = customerList;
			}
			List<SysCustomer> rtnList2 = new ArrayList<SysCustomer>();
			if(!StringUtil.isEmpty(nickName)){
				if(null != rtnList && rtnList.size() >0){
					for(SysCustomer sc : rtnList){
						if(sc.getCustomerShort().indexOf(nickName) >=0){
							rtnList2.add(sc);
						}
					}
				}
			}else{
				rtnList2 = rtnList;
			}
			List<SysCustomer> rtnList3 = new ArrayList<SysCustomer>();
			if(!"-1".equals(customerType)){
				if(null != rtnList2 && rtnList2.size() >0){
					for(SysCustomer sc : rtnList2){
						if(sc.getCustomerType().equals(customerType)){
							rtnList3.add(sc);
						}
					}
				}
			}else{
				rtnList3 = rtnList2;
			}
			PageModelUtil pdm = new PageModelUtil(rtnList3, numPerPage);
			sysCustomerList = (List<SysCustomer>) pdm.getCurrentPage(pageNum);
			totalCount = rtnList.size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
}
